package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-14
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {

        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();//条件构造器
        wrapper.eq("date_calculated",day);// 设置条件(根据统计日期查询)
        baseMapper.delete(wrapper);// 根据条件删除

        //远程调用得到某一天注册人数
        R registerR = ucenterClient.countRegister(day);
        Integer countRegister = (Integer)registerR.getData().get("countRegister");////查询一天的注册人数

        //把从表：ucenter_member 查询一天的注册人数放到表：statistics_daily
        StatisticsDaily sta = new StatisticsDaily();
        sta.setCourseNum(countRegister);//注册人数
        sta.setDateCalculated(day);//统计日期

        int i = org.apache.commons.lang3.RandomUtils.nextInt(100, 200);//随机数
        System.out.println("产生的随机数为：" + i);
        //RandomUtils.nextInt(100,200);
        sta.setVideoViewNum(i);//每日播放视频数
        sta.setLoginNum(i);//登录人数
        sta.setCourseNum(i);//每日新增课程数
        baseMapper.insert(sta);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();//条件构造器
        wrapper.between("date_calculated",begin,end);// 设置条件(根据统计日期查询)
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);// 根据条件删除

        //返回有两部分数据： 日期 和 数量，前端要求数据为Json结构
        //创建两个List集合，一个日期List,一个数量List
        ArrayList<String> date_calculatedList = new ArrayList<>();//日期集合
        ArrayList<Integer> numList = new ArrayList<>();//数量集合

        //遍历出来的对象，依次放入两个List集合中
        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily daily = staList.get(i);
            System.out.println("staList集合的全部数据：" + daily);
            //封装日期
            date_calculatedList.add(daily.getDateCalculated());
            //封装数量
            switch (type) {
                case "login_num"://登录人数
                    numList.add(daily.getLoginNum());
                    break;
                case "register_num"://注册人数
                    numList.add(daily.getRegisterNum());
                    break;
                case "video_view_num"://每日播放视频数
                    numList.add(daily.getVideoViewNum());
                    break;
                case "course_num"://每日新增课程数
                    numList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        //把封装之后两个List集合放到map集合进行返回
        HashMap<String, Object> map = new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numList",numList);
        return map;
    }
}
